package utils;

import com.google.common.collect.Lists;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @author yangjicong
 * @date 2022/6/8
 * steam工具
 */
public class StreamUtil {
    public static void main(String[] args) {

        DateTimeFormatter fmt = DateTimeFormatter.ofPattern(DateUtil.DATE_PATTERN1);

        String start = "2022-10-13 12:10:10";
        String end = "2022-10-13 13:10:10";
        LocalDate startTime = LocalDate.parse(start, fmt);
        LocalDate endTime = LocalDate.parse(end, fmt);
        long timeResult= endTime.toEpochDay()- startTime.toEpochDay();
        System.out.println("开始时间和结束时间相隔 [{}] 天 " + timeResult);
        int i = startTime.compareTo(endTime);
        System.out.println("i = " + i);
        if (timeResult < 0){
            System.out.println("时间有问题" );
        }else if (timeResult>365){
            System.out.println("时间间隔不能超过一年" );
        }

        ArrayList<Object> objects = Lists.newArrayList();

        boolean empty = CollectionUtils.isEmpty(objects);
        System.out.println("empty = " + empty);

        //double i = 103.11;
        ////if true, then 是整数 否则不是
        //boolean s= Math.round(i) == i;
        //System.out.println("s = " + s);
        //
        //
        //String ll = "1000.31";
        //System.out.println("Long.valueOf(ll) = " + (long)Double.valueOf(ll).intValue());

        //collectTest();

        //compareTest();
        //compareTest02();

        //listToMap();
        //test();
        //distinctList();
        //System.out.println("equals = " + equals);
        //这样不会报空指针
        //List<MerchantManageConditionBO> list = Optional.ofNullable(getNull()).orElse(Collections.EMPTY_LIST);
        //
        //for (MerchantManageConditionBO merchantManageConditionBO : list) {
        //    String openId = merchantManageConditionBO.getOpenId();
        //    System.out.println("merchantManageConditionBO = " + merchantManageConditionBO);
        //}

        //filter();
    }


    /**
     * 获取集合对象
     */
    public static List<MerchantManageConditionBO> getObject() {
        List<MerchantManageConditionBO> merchantManageConditionBOS = new ArrayList<>();
        //lombok构造器

        MerchantManageConditionBO m1 = new MerchantManageConditionBO();
        m1.setOpenId("11111111111");
        m1.setMobile("222222222222");
        m1.setMerchantName("白一");
        m1.setMerchantId("0");
        m1.setTadyDate(new Date());
        m1.setCreateAt("2022-09-02 13:09:51");
        m1.setReceiveType(Boolean.TRUE);

        merchantManageConditionBOS.add(m1);

        MerchantManageConditionBO m2 = new MerchantManageConditionBO();
        m2.setOpenId("2222222222");
        m2.setMobile("5555555555555");
        m2.setMerchantName("李二");
        m2.setMerchantId("1");
        m2.setTadyDate(new Date());
        m2.setCreateAt("2022-10-01 13:09:51");
        m2.setReceiveType(Boolean.TRUE);
        merchantManageConditionBOS.add(m2);

        MerchantManageConditionBO m3 = new MerchantManageConditionBO();
        m3.setOpenId("33333333333");
        m3.setMobile("56885856785");
        m3.setMerchantName("张三");
        m3.setMerchantId("2");
        m3.setTadyDate(new Date());
        m3.setCreateAt("2022-07-29 13:09:51");
        m3.setReceiveType(Boolean.FALSE);
        merchantManageConditionBOS.add(m3);

        MerchantManageConditionBO m4 = new MerchantManageConditionBO();
        m4.setOpenId("55555555555");
        m4.setMobile("234234");
        m4.setMerchantName("李四");
        m4.setMerchantId("4");
        m4.setTadyDate(new Date());
        m4.setCreateAt("2022-07-30 17:09:51");
        m4.setReceiveType(Boolean.FALSE);
        merchantManageConditionBOS.add(m4);

        MerchantManageConditionBO m5 = new MerchantManageConditionBO();
        m5.setOpenId("55555555555");
        m5.setMobile("23423423");
        m5.setMerchantName("王五");
        m5.setMerchantId("5");
        m5.setCreateAt("2022-07-28 23:09:51");
        m5.setTadyDate(new Date());
        m5.setReceiveType(Boolean.FALSE);
        merchantManageConditionBOS.add(m5);

        return merchantManageConditionBOS;

    }

    public static void grepTest(){
        List<MerchantManageConditionBO> object = getObject();
        Map<String, List<MerchantManageConditionBO>> collect = object.stream().collect(Collectors.groupingBy(MerchantManageConditionBO::getOpenId));

    }

    public static void compareTest(){
        List<MerchantManageConditionBO> object = getObject();
/*        object.sort(new Comparator<MerchantManageConditionBO>() {
            @Override
            public int compare(MerchantManageConditionBO o1, MerchantManageConditionBO o2) {
                return o2.getCreateAt().compareTo(o1.getCreateAt());
            }
        });
        for (MerchantManageConditionBO merchantManageConditionBO : object) {
            System.out.println("merchantManageConditionBO = " + merchantManageConditionBO.getCreateAt());
        }*/

        List<MerchantManageConditionBO> collect = object.stream().filter(e -> {
            if (e.getOpenId().equals("11111111111")) {
                return false;
            }
            return true;
        }).collect(Collectors.toList());


        System.out.println("collect = " + collect);

    }



    public static void compareTest02(){
        List<MerchantManageConditionBO> object = getObject();
        List<MerchantManageConditionBO> collect = object.stream()
                .filter(Objects::nonNull)
                .sorted((Comparator.comparing(MerchantManageConditionBO::getCreateAt, Comparator.nullsLast(Comparator.reverseOrder()))))
                .collect(Collectors.toList());
        for (MerchantManageConditionBO merchantManageConditionBO : collect) {

            System.out.println("merchantManageConditionBO = " + merchantManageConditionBO);
        }

    }


    /**
     * 收集为字符串
     */
    public static void collectTest(){
        List<MerchantManageConditionBO> object = getObject();
        String collect = object.stream().map(MerchantManageConditionBO::getMerchantName).collect(Collectors.joining(","));
        System.out.println("collect = " + collect);
    }



    /**
     * 过滤
     */
    public static void filter(){
        List<MerchantManageConditionBO> object = getObject();
        List<String> collect = object.stream().filter(e -> {
            return DateUtil.isToday(DateUtil.parseTo(e.getCreateAt(), DateUtil.DATE_PATTERN1));
        }).map(MerchantManageConditionBO::getMerchantName).collect(Collectors.toList());
        System.out.println("collect = " + collect);

    }


    public static void test(){
        List<MerchantManageConditionBO> result = getObject();
        for (MerchantManageConditionBO merchantManageConditionBO : result) {
            System.out.println("排序前 = " + merchantManageConditionBO);
        }
        //已领取放后面,未领取放前面
        for (int i = 0; i < result.size(); i++) {
            for (int j = 0; j < result.size() - 1 - i; j++) {
                //两两比较
                if (result.get(j).getReceiveType() && !result.get(j + 1).getReceiveType()) {
                    Collections.swap(result,j,j+1);
                }
            }
        }

        int returnSize = 3;
        result = result.size()>= returnSize ? result.subList(0,returnSize) : result;

        System.out.println("returnSize = " + result);
    }


    /**
     * list转为map集合
     */
    public static void listToMap(){
        List<MerchantManageConditionBO> list = getObject();
        ArrayList<MerchantManageConditionBO> merchantManageConditionBOS = Lists.newArrayList();
        Map<String, MerchantManageConditionBO> defaultConfigMap = merchantManageConditionBOS
                .stream()
                .collect(Collectors.toMap(MerchantManageConditionBO::getMerchantName, Function.identity()));

        MerchantManageConditionBO merchantManageConditionBO = defaultConfigMap.get("王五");
        System.out.println("merchantManageConditionBO = " + merchantManageConditionBO);

        for (Map.Entry<String, MerchantManageConditionBO> entry : defaultConfigMap.entrySet()) {

            System.out.println("key= " + entry.getKey() + " and value= \n"
                    + entry.getValue());
        }

    }
    public static List<MerchantManageConditionBO> getNull(){
        return null;
    }

    /**
     * 根据对象某个字段去重
     */
    public static void distinctList(){
        List<MerchantManageConditionBO> merchantManageConditionBOS = Optional.ofNullable(getNull()).orElse(Collections.EMPTY_LIST);

        //根据对象某个字段去重 -方式1
        List<MerchantManageConditionBO> collect = merchantManageConditionBOS.stream().filter(distinctByKey(MerchantManageConditionBO::getOpenId)).collect(Collectors.toList());

        //根据对象某个字段去重 方式2
        ArrayList<MerchantManageConditionBO> collect1 = merchantManageConditionBOS
                .stream()
                .collect(
                        Collectors.collectingAndThen(
                                Collectors.toCollection(
                                        () -> new TreeSet<>(
                                                Comparator.comparing(MerchantManageConditionBO::getOpenId)
                                        )
                                ), ArrayList::new
                        )
                );

        for (MerchantManageConditionBO merchantManageConditionBO : collect1) {
            System.out.println("merchantManageConditionBO = " + merchantManageConditionBO);
        }


    }

    /**
     * 分页工具类测试
     * @return
     */
    public static ListPageUtil<MerchantManageConditionBO> pageToList(){
        List<MerchantManageConditionBO> merchantManageConditionBOS = getObject();
        ListPageUtil<MerchantManageConditionBO> list = new ListPageUtil(merchantManageConditionBOS,3,2);

        int totalCount = list.getTotalCount();
        System.out.println("总数据条数 = " + totalCount);
        int totalPage = list.getTotalPage();
        System.out.println("总页数 = " + totalPage);

        int lastPage = list.getLastPage();
        System.out.println("上一页 = " + lastPage);
        int nextPage = list.getNextPage();
        System.out.println("下一页 = " + nextPage);
        int nowPage = list.getNowPage();
        System.out.println("当前页 = " + nowPage);

        List<MerchantManageConditionBO> pagedList = list.getPagedList();
        System.out.println("得到分页后的数据 = " + pagedList);


        return null;
    }


    public static void pageToList1(){
        List<MerchantManageConditionBO> merchantManageConditionBOS = getObject();
        merchantManageConditionBOS.stream().sorted(Comparator.comparing(MerchantManageConditionBO::getCreateAt)).forEach(System.out::println);
    }



    private static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
        ConcurrentHashMap<Object, Boolean> map = new ConcurrentHashMap<>();
        return t -> map.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }





}
